package com.qf.j2112.config;

import com.qf.j2112.util.RedisUtilSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ProjectName: YiZhanXiYi
 * @Package: com.qf.j2112.config
 * @ClassName: RedisConfig
 * @Author: 宋晓博
 * @Description: description
 * @Date: 2022/4/22 10:45
 * @Version: 1.0
 */

@Configuration
public class RedisConfig {

    @Bean  //手工注入一个Jedis是Connection Factory的对象
    public JedisConnectionFactory getJedisConnectionFactory(){
        return new JedisConnectionFactory() ;
    }

    @Bean
    public RedisTemplate redisTemplate(){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(getJedisConnectionFactory());
//        设值处理string类型数据的序列号转换器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new RedisUtilSerializer());//value可以存储任意对象
//        设值处理hash类型数据的序列号转换器
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new RedisUtilSerializer());//value配置自定义的序列化转化器
        return redisTemplate;
    }
}
